/*
 * Name: Myles Dale
 * Class: UpdateProfile.java
 */

package actions;

import java.io.IOException;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import data.UserDB;
import beans.*;

public class UpdateProfile extends Action {

	@Override
	protected void setURLMapping() {
		urlMapping = "/UpdateProfile";
		
	}

	@Override
	protected void setupPage(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
	}

	@Override
	protected String doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String url = "/WEB-INF/views/content/UpdateProfile.jsp";
		User user;
		int userID = Integer.parseInt(request.getParameter("userID"));
		user = UserDB.getUser(userID);
		
		if (user != null)
		{
			request.setAttribute("user", user);
		}
		return url;
	}

	@Override
	protected String doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String url = "Home";
		int status;
		
		User user = new User();
		//load User bean with data
		user.setUserid(Integer.parseInt(request.getParameter("userID")));
		user.setAddress1(request.getParameter("address1"));
		user.setAddress2(request.getParameter("address2"));
		user.setCity(request.getParameter("city"));
		String year = request.getParameter("dobYear");  
		String month = request.getParameter("dobMonth"); 
		String day = request.getParameter("dobDay");     
		java.sql.Date date = java.sql.Date.valueOf(year + "-" + month + "-" + day);
		user.setDob(date);
		user.setEmail(request.getParameter("email"));
		user.setFirstName(request.getParameter("firstName"));
		user.setGender(Boolean.parseBoolean(request.getParameter("gender")));
		user.setLastName(request.getParameter("lastName"));
		user.setPassword(request.getParameter("password"));
		user.setPhone(request.getParameter("phone"));
		user.setPicturePath("images/no-avatar.jpg");
		user.setState(request.getParameter("state"));
		user.setUserName(request.getParameter("userName"));
		user.setZip(request.getParameter("zip"));
		
		
		status = UserDB.updateUser(user);
		
		if (status == 1) //add successfully
		{
			url = "Profile";
		}
		return url;
	}

}
